R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

setwd("c:/FRTB/_Portfolio_01") 
# load a workspace into the current session
# if you don't specify the path, the cwd is assumed 
load("myfile.RData")

Including Plots

You can also embed plots, for example:

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
covmat <- matrix(c(0.8, 0.4, 0.3, 0.8), nrow = 2, byrow = T)
df <- mvrnorm(n = 10000, c(0,0), Sigma = covmat)
df <- as.data.frame(df)

colnames(df) <- c("x", "y")
p <- plot_ly(df, x = ~rates[1:3600,1], y = ~rates[1:3600,2]) %>%
  add_markers(marker = list(line = list(color = "black", width = 1))) %>%
  layout(
    title = "Drop down menus - Plot type",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.8,
        buttons = list(

          list(method = "restyle",
               args = list("type", "scatter"),
               label = "Scatter"),

          list(method = "restyle",
               args = list("type", "histogram2d"),
               label = "2D Histogram")))
    ))

p
## Warning: Ignoring 1 observations

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(cranlogs)
library(plotly)
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# Get data
df <- cran_downloads(packages = c("plotly", "ggplot2"), 
                     from = "2015-10-01", to = "2016-08-01")
 
# Convert dates
df$date <- as.Date(df$date)
 
# Make data frame
df <- data.frame(date = unique(df$date),
                 count.plotly = subset(df, package == "plotly")$count,
                 count.ggplot = subset(df, package == "ggplot2")$count)
 
# Smooth data 
# 5 day moving averages
nWidth <- 5
df <- zoo(df[,-1], order.by = df[,1])
df.smooth <- rollapply(df, width = nWidth, FUN = mean)
 
df <- data.frame(date = index(df.smooth),
                 count.plotly = round(df.smooth[,1],0),
                 count.ggplot = round(df.smooth[,2],0))
 
plot_ly(df, x = ~date) %>% 
  add_lines(y = ~count.plotly, fill = "tozeroy", line = list(shape = "spline"), name = "Plotly") %>% 
  add_lines(y = ~count.ggplot, fill = "tozeroy", line = list(shape = "spline"), name = "ggplot2", 
            yaxis = "y2", xaxis = "x2") %>% 
  
  layout(yaxis = list(domain = c(0.55,1), title = "Count", anchor = "xaxis",
                      tickfont = list(color = "#595959", size = 12)),
         
         yaxis2 = list(domain = c(0, 0.45), title = "Count", anchor = "xaxis2",
                       tickfont = list(color = "#595959", size = 12)),
         
         xaxis = list(anchor = "yaxis", title = "Date"),
         
         xaxis2 = list(anchor = "free", title = "Date"),
         
         annotations = list(
           
           list(x = 0.05, y = 1, xref = "paper", yref = "paper",
                showarrow = FALSE,
                text = "<b>Plotly downloads</b>",
                font = list(size = 17)),
           
           list(x = 0.05, y = 0.45, xref = "paper", yref = "paper",
                showarrow = FALSE,
                text = "<b>ggplot2 downloads</b>",
                font = list(size = 17))))
# Get this figure: fig <- get_figure("chelsea_lyn", 12871)
# Get this figure's data: data <- get_figure("chelsea_lyn", 12871)$data
# Add data to this figure: p <- add_trace(p, x=c(4, 5), y=c(4, 5), kwargs=list(filename="plot from API (1000) (475)", fileopt="extend"))
# Get y data of first trace: y1 <- get_figure("chelsea_lyn", 12871)$data[[1]]$y

# Get figure documentation: https://plot.ly/r/get-requests/
# Add data documentation: https://plot.ly/r/file-options/

# You can reproduce this figure in R with the following code!

# Learn about API authentication here: https://plot.ly/r/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)
trace1 <- list(
  x = c("United States", "China", "South Korea", "Hungary", "Austraila", "Canada"), 
  y = c(10, 8, 4, 4, 4, 0), 
  line = list(
    color = "#FFD700", 
    width = 3
  ), 
  name = "Gold", 
  type = "scatter"
)
trace2 <- list(
  x = c("United States", "China", "South Korea", "Hungary", "Austraila", "Canada"), 
  y = c(8, 3, 2, 1, 0, 1), 
  line = list(
    color = "#C0C0C0", 
    width = 3
  ), 
  name = "Silver", 
  type = "scatter"
)
trace3 <- list(
  x = c("United States", "China", "South Korea", "Hungary", "Austraila", "Canada"), 
  y = c(9, 6, 1, 1, 5, 4), 
  line = list(
    color = "#BA8651", 
    width = 3
  ), 
  name = "Bronze", 
  type = "scatter"
)
trace4 <- list(
  x = c("United States", "China", "South Korea", "Hungary", "Austraila", "Canada"), 
  y = c(27, 17, 7, 6, 9, 5), 
  line = list(
    color = "#000000", 
    width = 4
  ), 
  name = "Total", 
  type = "scatter"
)
data <- list(trace1, trace2, trace3, trace4)
layout <- list(
  title = "2016 Summer Olympic Medal Count", 
  updatemenus = list(
    list(
      x = -0.05, 
      y = 1, 
      buttons = list(
        list(
          args = c("visible", c(TRUE, TRUE, TRUE, TRUE)), 
          label = "All", 
          method = "restyle"
        ), 
        list(
          args = c("visible", c(TRUE, FALSE, FALSE, FALSE)), 
          label = "Gold", 
          method = "restyle"
        ), 
        list(
          args = c("visible", c(FALSE, TRUE, FALSE, FALSE)), 
          label = "Silver", 
          method = "restyle"
        ), 
        list(
          args = c("visible", c(FALSE, FALSE, TRUE, FALSE)), 
          label = "Bronze", 
          method = "restyle"
        ), 
        list(
          args = c("visible", c(FALSE, FALSE, FALSE, TRUE)), 
          label = "Total", 
          method = "restyle"
        )
      ), 
      yanchor = "top"
    )
  )
)
p <- plot_ly()
p <- add_trace(p, x=trace1$x, y=trace1$y, line=trace1$line, name=trace1$name, type=trace1$type)
p <- add_trace(p, x=trace2$x, y=trace2$y, line=trace2$line, name=trace2$name, type=trace2$type)
p <- add_trace(p, x=trace3$x, y=trace3$y, line=trace3$line, name=trace3$name, type=trace3$type)
p <- add_trace(p, x=trace4$x, y=trace4$y, line=trace4$line, name=trace4$name, type=trace4$type)
p <- layout(p, title=layout$title, updatemenus=layout$updatemenus)
p
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
library(DT)
datatable(iris, options = list(pageLength = 5))
library(gridExtra)
library(ggplot2)
library(reshape2)

dataset=read.table("C:/OneDrive/8_PowerBI/WriteTable/Allocation.csv",sep = ",")
d <-dataset
p<-ggplot(data=d,mapping=aes(x=d[,1],y=d[,2]))
p<-p+geom_boxplot()
p

e=dcast(d,Date~Symbol,value.var=colnames(d)[3])
  

 #Plot your table with table Grob in the library(gridExtra)
ss <- tableGrob(e)

 #Arrange them as you want with grid.arrange
   grid.arrange(p,ss)